﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
using System.Text.RegularExpressions;

namespace BaHuan
{
    public partial class FrmChiNhanhKhachHang : Form
    {
        private  ChiNhanhKhachHangManagement chinhanhkhachhangMgt = new ChiNhanhKhachHangManagement();
        private KhachHang khachhang;
        public KhachHang KhachHang
        {
            set { this.khachhang = value; }
            get { return this.khachhang; }
        }
        
        private bool IsEdit = false;

        public FrmChiNhanhKhachHang()
        {
            InitializeComponent();
            
        }
        public void ChangeStatusTextBox(bool IsReadOnly)
        {
            if (IsReadOnly)
            {
                txtTenKhachHang.ReadOnly = true;
                txtDiaChi.ReadOnly = true;
                txtEmail.ReadOnly = true;
                txtSoDienThoai.ReadOnly = true;
                txtSoFax.ReadOnly = true;
                txtMaThue.ReadOnly = true;
                txtChietKhau.ReadOnly = true;
            }
            else
            {
                
                txtTenKhachHang.ReadOnly = false;
                txtDiaChi.ReadOnly = false;
                txtEmail.ReadOnly = false;
                txtSoDienThoai.ReadOnly = false;
                txtSoFax.ReadOnly = false;
                txtMaThue.ReadOnly = false;
                txtChietKhau.ReadOnly = false;
            }
        }
        public void ClearTextBox()
        {
            txtTenKhachHang.Clear();
            txtDiaChi.Clear();
            txtEmail.Clear();
            txtSoDienThoai.Clear();
            txtSoFax.Clear();
            txtMaThue.Clear();
            txtChietKhau.Clear();
        }

        public void BindingToGrid()
        {
            ResultObj result = new ResultObj();
            result = chinhanhkhachhangMgt.GetAllChiNhanhKhachHang();
            List<ChiNhanhKhachHang> lstChiNhanhKhachHang = (from cn in (List<ChiNhanhKhachHang>)result.Result
                                                            where cn.IdKhachHang == this.khachhang.Id
                                                            select cn).ToList();
            dgvChiNhanhKhachHang.DataSource = lstChiNhanhKhachHang;

            
        }

        private void FrmChiNhanhKhachHang_Load(object sender, EventArgs e)
        {
            lblID.Hide();
            groupBox1.Enabled = false;
            dgvChiNhanhKhachHang.AutoGenerateColumns = false;
            BindingToGrid();
            btnXoa.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = false;
        }
  
        private void btnThem_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            ClearTextBox();
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            IsEdit = false;
            txtTenKhachHang.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            if (txtTenKhachHang.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên chi nhánh !","Thông báo");
            }
            else
            {

                btnThem.BackColor = Color.Empty;
                btnSua.BackColor = Color.Empty;

                ResultObj result = new ResultObj();
                int nIdKhachHang = this.khachhang.Id;
                string sTen = txtTenKhachHang.Text;
                string sDiaChi = txtDiaChi.Text;
                string sEmail = txtEmail.Text;
                string sSoDT = txtSoDienThoai.Text;
                string sFax = txtSoFax.Text;
                string sMaThue = txtMaThue.Text;
                double nChietKhau = Convert.ToDouble(txtChietKhau.Text);

                ChiNhanhKhachHang chinhanhkhachhang = new ChiNhanhKhachHang();
                chinhanhkhachhang.IdKhachHang = nIdKhachHang;
                chinhanhkhachhang.Ten = sTen;
                chinhanhkhachhang.DiaChi = sDiaChi;
                chinhanhkhachhang.Email = sEmail;
                chinhanhkhachhang.DienThoai = sSoDT;
                chinhanhkhachhang.Fax = sFax;
                chinhanhkhachhang.MaSoThue = sMaThue;
                chinhanhkhachhang.ChietKhau = nChietKhau;

                if (!IsEdit)
                {
                    result = chinhanhkhachhangMgt.SaveNewChiNhanhKhachHang(chinhanhkhachhang);
                }
                else
                {
                    chinhanhkhachhang.Id = Convert.ToInt32(lblID.Text);
                    result = chinhanhkhachhangMgt.UpdateChiNhanhKhachHang(chinhanhkhachhang);
                    IsEdit = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");
                    FrmChiNhanhKhachHang_Load(null, null);
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTenKhachHang.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTenKhachHang.Select();
                }

                BindingToGrid();
            }
        }

        private void dgvChiNhanhKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                btnXoa.Enabled = false;
                ChangeStatusTextBox(true);
                lblID.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTenKhachHang.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtDiaChi.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtEmail.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtSoDienThoai.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtSoFax.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtMaThue.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[7].Value.ToString();
                txtChietKhau.Text = dgvChiNhanhKhachHang.Rows[e.RowIndex].Cells[8].Value.ToString();
            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            IsEdit = true;
            groupBox1.Enabled = true;
            ChangeStatusTextBox(false);
            btnThem.Enabled = true;
            btnLuu.Enabled = true;
            btnXoa.Enabled = false;
            txtTenKhachHang.Select();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            FrmTimKiemChiNhanh frm = new FrmTimKiemChiNhanh();
            frm.Show();
            
        }

        private void txtChietKhau_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !","Thông báo");
            }

        }

        private void txtSoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }

        private void txtSoFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !", "Thông báo");
            }
        }
    }
}
